#include "Log.hpp"
#include "InetAddr.hpp"
#include "Socket.hpp"
#include "protocol.hpp"

#include <memory>

enum
{
    USAGEERROR = 1
};



// ./client 127.0.0.1 8080
int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        std::cout << "Usage: " << "\r\n\t" << argv[0] << " [ip] [port]" << std::endl;
        return USAGEERROR;
    }
    srand(time(nullptr) ^ getpid());
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    // std::cout << "ip: " << ip << ", port: " << port << std::endl;
    std::shared_ptr<inetaddr::InetAddr> ineta = std::make_shared<inetaddr::InetAddr>(inetaddr::InetAddr(ip, port));
    std::shared_ptr<sock::TcpSocket> ts = std::make_shared<sock::TcpSocket>(sock::TcpSocket(ip, port));

    ts->CreateSocket();
    ts->Connect(ineta->getsocket(), sizeof(ineta->getsocket()));
    
    Factory fac;
    for(int i = 0; i < 5; ++i)
    {
        std::shared_ptr<Request> req = fac.BuildRequest();

        ts->Send(Encode(req->serialize()));
    }
    ts->Close();

    return 0;
}